<?php

/* 属性 attribute */
class AttributeModel extends BaseModel
{
	var $table  = 'attribute';
	var $prikey = 'attr_id';
	var $_name  = 'attribute';
	/* 添加编辑时自动验证 */
	var $_autov = array(
	'attr_name' => array(
	'required'  => true,    //必填
	'min'       => 1,       //最短1个字符
	'max'       => 100,     //最长100个字符
	'filter'    => 'trim',
	),
	'sort_order'  => array(
	'filter'    => 'intval',
	)
	);


	function get_list($cid,$attr_index = -1)
	{
	   if(isset($attr_index) && $attr_index != -1)
       {
          $sql = "SELECT a.*,c.cate_name FROM {$this->table} a left join " . DB_PREFIX ."gcategory c on a.cate_id = c.cate_id  where a.cate_id = $cid and a.attr_index = {$attr_index} ORDER BY a.sort_order ASC";
        
       }
       else
       {
          $sql = "SELECT a.*,c.cate_name FROM {$this->table} a left join " . DB_PREFIX ."gcategory c on a.cate_id = c.cate_id  where a.cate_id = $cid ORDER BY a.sort_order ASC";
       }
		
		return $this->db->getAll($sql);
	}
	/**
     *    删除商品屬性
     *
     *    @author    Hyber
     *    @param     string $conditions
     *    @param     string $fields
     *    @return    void
     */
	function drop($conditions,$fields = '')
	{
		$droped_rows = parent::drop($conditions, $fields);

		return $droped_rows;
	}
	/*
	* 判断名称是否唯一
	*/
	function unique($attr_name, $cate_id,$attr_id = 0)
	{
		//echo "aa";die();
		
		$conditions = "attr_name = '" . $attr_name . "' AND cate_id = ".$cate_id." AND attr_id != ".$attr_id."";
		//dump($conditions);
		return count($this->find(array('conditions' => $conditions))) == 0;
	}
}
?>